package com.aye.notepad.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.aye.notepad.DatabaseModel.User;
import com.aye.notepad.R;

import org.litepal.crud.DataSupport;

import java.util.List;

public class UserActivity extends AppCompatActivity {
    private EditText et_name,et_pwd,et_phone,et_date;
    private RadioButton rb_man,rb_woman;
    private String sex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        Intent intent=getIntent();
        String username=intent.getStringExtra("name");
        init();
        List<User> userList = DataSupport.where("name=?", username).find(User.class);
        for (User user:userList){
            et_name.setText(user.getName());
            et_pwd.setText(user.getPassword());
            et_phone.setText(user.getPhone());
            et_date.setText(user.getBirthday());
            sex=user.getSex();
        }
        if (sex.equals("男")){
            rb_man.setChecked(true);
        }else {
            rb_woman.setChecked(true);
        }

    }
    /*********************************************修改个人信息****************************************/
    public void update(View v){
        //点击确认按钮时进行信息验证
        String name = et_name.getText().toString();
        String pwd = et_pwd.getText().toString();
        String phone = et_phone.getText().toString();
        String date=et_date.getText().toString();
        String sex = null;
        if (rb_man.isChecked() == true) {
            sex = "男";
        } else if (rb_woman.isChecked()==true){
            sex = "女";
        }
        if (name != null && pwd != null && phone != null && date != null && sex != null) {
            User user=new User(name,pwd,phone,date,sex);
            user.updateAll("name=?",name);
                showMsg("修改成功");
                finish();
        }else{
            showMsg("修改失败，有内容不合法");
        }
    }
        public void cannel(View v){
        finish();
        }
/****************************自定义方法************************/
    private void init() {
        et_name=findViewById(R.id.et_name);
        et_pwd=findViewById(R.id.et_pwd);
        et_phone=findViewById(R.id.et_phone);
        rb_man=findViewById(R.id.rb_man);
        rb_woman=findViewById(R.id.rb_woman);
        et_date=findViewById(R.id.et_date);
    }

    private void showMsg(String s) {
        Toast.makeText(UserActivity.this,s,Toast.LENGTH_SHORT).show();
    }
}
